package com.ebeitech.library.util;

import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * Created by alice on 2017/10/25.
 */
public class FileUtil {

	/**
	 * 根据指定的路径创建文件
	 *
	 * @param filepath
	 * @return
	 * @throws IOException
	 */
	public static File createFile(String filepath) throws IOException {
		int last_seperate = filepath.lastIndexOf("/") + 1;
		String path = filepath.substring(0, last_seperate);
		String name = filepath.substring(last_seperate);
		return createFile(path, name);
	}

	/**
	 * 由指定的路径和文件名创建文件
	 *
	 * @param path
	 * @param name
	 * @return
	 * @throws IOException
	 */
	public static File createFile(String path, String name) throws IOException {
		File folder = new File(path);
		if (!folder.exists()) {
			folder.mkdirs();
		}
		File file = new File(path + name);
		if (!file.exists()) {
			file.createNewFile();
		}
		return file;
	}

	/**
	 * 删除指定文件
	 *
	 * @param path
	 * @param name
	 */
	public static void deleteFile(String path, String name) {
		if (!fileExist(path, name)) {
			return;
		}
		File file = new File(path + name);
		file.delete();
	}

	/**
	 * 判断文件是否存在
	 *
	 * @param path
	 * @param name
	 * @return
	 */
	public static boolean fileExist(String path, String name) {
		File file = new File(path + name);
		if (file.exists() & !file.isDirectory()) {
			return true;
		}
		return false;
	}
	/**
	 * 判断文件是否存在
	 *
	 * @param path
	 * @return
	 */
	public static boolean fileExist(String path) {
		File file = new File(path);
		if (file.exists() & !file.isDirectory()) {
			return true;
		}
		return false;
	}

	/**
	 * 拷贝文件
	 *
	 * @param srcPath
	 * @param srcName
	 * @param desPath
	 * @param desName
	 * @return boolean
	 */
	public static boolean copyFile(String srcPath, String srcName, String desPath, String desName) {
		if (!fileExist(srcPath, srcName)) {
			return false;
		}

		FileInputStream fis = null;
		BufferedInputStream bis = null;
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
		try {
			File inFile = new File(srcPath, srcName);
			File outFile = new File(desPath, desName);

			if (!fileExist(desPath, desName)) {
				createFile(desPath, desName);
			}

			fis = new FileInputStream(inFile);
			bis = new BufferedInputStream(fis);

			fos = new FileOutputStream(outFile);
			bos = new BufferedOutputStream(fos);

			byte[] buffer = new byte[1024 * 8];
			int len = -1;
			while ((len = fis.read(buffer)) != -1) {
				bos.write(buffer, 0, len);
			}
			bos.flush();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeStream(bis);
			closeStream(fis);
			closeStream(bos);
			closeStream(fos);
		}
		return false;
	}

	/**
	 * 拷贝文件
	 *
	 * @param srcPath
	 * @param desPath
	 * @return boolean
	 */
	public static boolean copyFile(String srcPath, String desPath) {
		if (!fileExist(srcPath)) {
			return false;
		}

		FileInputStream fis = null;
		BufferedInputStream bis = null;
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
		try {
			File inFile = new File(srcPath);
			File outFile = new File(desPath);

			if (!fileExist(desPath)) {
				createFile(desPath);
			}

			fis = new FileInputStream(inFile);
			bis = new BufferedInputStream(fis);

			fos = new FileOutputStream(outFile);
			bos = new BufferedOutputStream(fos);

			byte[] buffer = new byte[1024 * 8];
			int len = -1;
			while ((len = fis.read(buffer)) != -1) {
				bos.write(buffer, 0, len);
			}
			bos.flush();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeStream(bis);
			closeStream(fis);
			closeStream(bos);
			closeStream(fos);
		}
		return false;
	}

	/**
	 * Close outputStream
	 *
	 * @param stream
	 */
	public static void closeStream(Closeable stream) {
		if (stream != null) {
			try {
				stream.close();
				stream = null;
			} catch (IOException e) {
				Log.e("e",e.getMessage());
			} finally {
				if (stream != null) {
					try {
						stream.close();
					} catch (IOException e) {
						stream = null;
						Log.e("e",e.getMessage());
					}
					stream = null;
				}
			}
		}
	}

	/**
	 * 写入文件
	 *
	 * @param dataPath
	 * @param data
	 * @throws IOException
	 */
	public static synchronized void saveData(String dataPath, String data) throws IOException {
		File file = createFile(dataPath);
		RandomAccessFile raf = new RandomAccessFile(file, "rws");
		byte[] byteds = data.getBytes();
		raf.seek(file.length());
		raf.write(byteds);
		closeStream(raf);
	}

}
